package com.first.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.order.dao.OrderDao;
import com.first.common.pojo.Order;
import com.first.order.service.OrderService;
import com.first.order.vo.OrderVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @program: SecondProject
 * @description:
 * @author: 李榕轩
 * @create: 2023-05-26 16:39
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;

    @Override
    public Page<Order> findByPage(Integer currentPage, Integer pageSize, OrderVo orderVo) {
        Page<Order> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        if (StringUtils.hasText(orderVo.getMobile())) {
            wrapper.like("mobile", orderVo.getMobile());
        }
        if (StringUtils.hasText(orderVo.getOrderNo())) {
            wrapper.like("order_no", orderVo.getOrderNo());
        }
        if (StringUtils.hasText(orderVo.getCourseTitle())) {
            wrapper.like("course_title", orderVo.getCourseTitle());
        }
        if (StringUtils.hasText(orderVo.getGmtCreate())) {
            wrapper.like("gmt_create", orderVo.getGmtCreate());
        }
        if (StringUtils.hasText(orderVo.getGmtModified())) {
            wrapper.like("gmt_modified", orderVo.getGmtModified());
        }
        if (StringUtils.hasText(orderVo.getStatus())) {
            wrapper.like("status", orderVo.getStatus());
        }
        if (StringUtils.hasText(orderVo.getTeacherName())) {
            wrapper.like("teacher_name", orderVo.getTeacherName());
        }
        if (StringUtils.hasText(orderVo.getPayType())) {
            wrapper.like("pay_type", orderVo.getPayType());
        }
        Page<Order> orderPage = orderDao.selectPage(page, wrapper);
        return orderPage;
    }

    @Override
    public Double selectMoney() {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        List<Order> orders = orderDao.selectList(wrapper);
        BigDecimal i = BigDecimal.valueOf(0);
        for (Order order : orders) {

            BigDecimal money = BigDecimal.valueOf(Double.valueOf(order.getTotalFee()));
            i = i.add(money);

        }
        return i.doubleValue();
    }

    @Override
    public Order getOrderStatus(String id, String memberId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", id);
        wrapper.eq("member_id",memberId);
        return orderDao.selectOne(wrapper);
    }

    @Override
    public Integer deleteOrder(String id) {
        int i = orderDao.deleteById(id);
        return i;
    }
}
